//
//  HWSaveMoneyCardCouponCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/29.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit

class HWSaveMoneyCardCouponCell: UITableViewCell {
    var bgImageView:UIImageView!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    
    var collectionView:UICollectionView!
    var model:HWSaveModeyCardModel? {
        didSet {
            self.collectionView.reloadData()
            
            if model?.selectZhou == true {
                titleLabel.text = "开通超级省钱卡，\(model?.zhou_data.usenum_day ?? "") 天内均可使用"
                contentLabel.text = "全游戏通用劵 总价值超过：\(model?.zhou_data.zongji ?? "")"
            } else {
                titleLabel.text = "开通超级省钱卡，\(model?.yue_data.usenum_day ?? "") 天内均可使用"
                contentLabel.text = "全游戏通用劵 总价值超过：\(model?.yue_data.zongji ?? "")"
            }
            
            self.collectionView.reloadData()
        }
    }
    
    var bottomButtonClick:((_:String,_:String)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWSaveMoneyCardCouponCell) {
        let reUse:String = "HWSaveMoneyCardCouponCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWSaveMoneyCardCouponCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWSaveMoneyCardCouponCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        contentView.backgroundColor = UIColor.colorWithHexString(hexString: "#272727")
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "saveMoneyCouponBg")
        bgImageView.isUserInteractionEnabled = true
        contentView.addSubview(bgImageView)
        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#EEEEEE", font: 16*widthScale)
        bgImageView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#EEEEEE", font: 13*widthScale)
        bgImageView.addSubview(contentLabel)
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 7*widthScale
        flowL.minimumInteritemSpacing = 7*widthScale
        flowL.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(HWSaveMoneyCouponCollentionCell.self, forCellWithReuseIdentifier: "HWSaveMoneyCouponCollentionCell")
        bgImageView.addSubview(collectionView)
    }
    
    func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(181*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(20*widthScale)
            make.height.equalTo(23*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(titleLabel.snp.bottom).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(contentLabel.snp.bottom).offset(9*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
extension HWSaveMoneyCardCouponCell:UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 18*widthScale, bottom: 0, right: 18*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 9*widthScale
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 9*widthScale
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if model?.selectZhou == true {
            return model?.zhou_data.coupon.count ?? 0
        }else{
            return model?.yue_data.coupon.count ?? 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: 69*widthScale, height: 87*widthScale)
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWSaveMoneyCouponCollentionCell", for: indexPath) as! HWSaveMoneyCouponCollentionCell
        cell.indexPath = indexPath
        cell.model = model
        cell.bottomButtonClick = {
            self.bottomButtonClick?($0,$1)
        }
        return cell
    }
    
    
}
class HWSaveMoneyCouponCollentionCell:UICollectionViewCell {
    var bgImageView:UIImageView!
    var titleLabel:UILabel!
    var priceLabel:UILabel!
    var bottomButton:UIButton!
    var indexPath:IndexPath!
    //    var model:HWSaveModeyCardCouponModel? {
    //        didSet {
    //            if indexPath.row == 0 {
    //                titleLabel.text = "开通即领"
    //            }else{
    //                titleLabel.text = "第\(model?.day ?? "")天"
    //            }
    //
    //            priceLabel.text = "￥\(model?.jian ?? "")"
    ////            bottomButton.setTitle("满\(model?.man ?? "")减", for: .normal)
    //        }
    //    }
    
    var model:HWSaveModeyCardModel? {
        didSet {
            bottomButton.layoutIfNeeded()
            if model?.is_buy == "1" {
                
                if model?.selectZhou == true {
                    if indexPath.row == 0 {
                        titleLabel.text = "开通即领"
                    }else{
                        titleLabel.text = "第\(model?.zhou_data.coupon[indexPath.row].day ?? "")天"
                    }
                    priceLabel.text = "￥\(model?.zhou_data.coupon[indexPath.row].jian ?? "")"
                    
                    if model?.zhou_data.coupon[indexPath.row].is_lq == "1" {
                        bottomButton.setTitle("已领取", for: .normal)
                        bottomButton.setTitleColor(.colorWithHexString(hexString: "#272727"), for: .normal)
                        UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                           UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    } else if model?.zhou_data.coupon[indexPath.row].is_lq == "2" {
                        bottomButton.setTitle("已过期", for: .normal)
                        bottomButton.setTitleColor(.colorWithHexString(hexString: "#272727"), for: .normal)
                        UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                           UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    } else {
                        bottomButton.setTitle("领取", for: .normal)
                        bottomButton.setTitleColor(.colorWithHexString(hexString: "#502E05"), for: .normal)
                        UIColor.gradientColor(view: bottomButton, colors: [UIColor.colorWithHexString(hexString: "#FDC013").cgColor,
                                                                           UIColor.colorWithHexString(hexString: "#FA8C08").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    }
                    bottomButton.isEnabled = true
                   
                } else {
                    if indexPath.row == 0 {
                        titleLabel.text = "开通即领"
                    }else{
                        titleLabel.text = "第\(model?.yue_data.coupon[indexPath.row].day ?? "")天"
                    }
                    priceLabel.text = "￥\(model?.yue_data.coupon[indexPath.row].jian ?? "")"
                    UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                       UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    if indexPath.row == 0 {
                        bottomButton.setTitle((model?.yue_data.coupon[indexPath.row].ktjs_name ?? ""), for: .normal)
                    } else {
                        bottomButton.setTitle((model?.yue_data.coupon[indexPath.row].mrzl_name ?? ""), for: .normal)
                    }
                    
                    bottomButton.isEnabled = false
                }
            } else if model?.is_buy == "2" {
                
                if model?.selectZhou == true {
                    if indexPath.row == 0 {
                        titleLabel.text = "开通即领"
                    }else{
                        titleLabel.text = "第\(model?.zhou_data.coupon[indexPath.row].day ?? "")天"
                    }
                    priceLabel.text = "￥\(model?.zhou_data.coupon[indexPath.row].jian ?? "")"
                    UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                       UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    if indexPath.row == 0 {
                        bottomButton.setTitle((model?.zhou_data.coupon[indexPath.row].ktjs_name ?? ""), for: .normal)
                    } else {
                        bottomButton.setTitle((model?.zhou_data.coupon[indexPath.row].mrzl_name ?? ""), for: .normal)
                    }
                    bottomButton.isEnabled = false
                }else {
                    if indexPath.row == 0 {
                        titleLabel.text = "开通即领"
                    }else{
                        titleLabel.text = "第\(model?.yue_data.coupon[indexPath.row].day ?? "")天"
                    }
                    priceLabel.text = "￥\(model?.yue_data.coupon[indexPath.row].jian ?? "")"
                    if model?.yue_data.coupon[indexPath.row].is_lq == "1" {
                        bottomButton.setTitle("已领取", for: .normal)
                        bottomButton.setTitleColor(.colorWithHexString(hexString: "#272727"), for: .normal)
                        UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                           UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    } else if model?.yue_data.coupon[indexPath.row].is_lq == "2" {
                        bottomButton.setTitle("已过期", for: .normal)
                        bottomButton.setTitleColor(.colorWithHexString(hexString: "#272727"), for: .normal)
                        UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                           UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    } else {
                        bottomButton.setTitle("领取", for: .normal)
                        bottomButton.setTitleColor(.colorWithHexString(hexString: "#502E05"), for: .normal)
                        UIColor.gradientColor(view: bottomButton, colors: [UIColor.colorWithHexString(hexString: "#FDC013").cgColor,
                                                                           UIColor.colorWithHexString(hexString: "#FA8C08").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                    }
                    bottomButton.isEnabled = true
                }
                
            } else {
                if model?.selectZhou == true {
                    if indexPath.row == 0 {
                        titleLabel.text = "开通即领"
                    }else{
                        titleLabel.text = "第\(model?.zhou_data.coupon[indexPath.row].day ?? "")天"
                    }
                    priceLabel.text = "￥\(model?.zhou_data.coupon[indexPath.row].jian ?? "")"
                    if indexPath.row == 0 {
                        bottomButton.setTitle((model?.zhou_data.coupon[indexPath.row].ktjs_name ?? ""), for: .normal)
                    } else {
                        bottomButton.setTitle((model?.zhou_data.coupon[indexPath.row].mrzl_name ?? ""), for: .normal)
                    }
                } else {
                    if indexPath.row == 0 {
                        titleLabel.text = "开通即领"
                    }else{
                        titleLabel.text = "第\(model?.yue_data.coupon[indexPath.row].day ?? "")天"
                    }
                    if indexPath.row == 0 {
                        bottomButton.setTitle((model?.yue_data.coupon[indexPath.row].ktjs_name ?? ""), for: .normal)
                    } else {
                        bottomButton.setTitle((model?.yue_data.coupon[indexPath.row].mrzl_name ?? ""), for: .normal)
                    }
                    priceLabel.text = "￥\(model?.yue_data.coupon[indexPath.row].jian ?? "")"
                }
                UIColor.gradientColor(view: bottomButton, colors: [UIColor.clear.cgColor,
                                                                   UIColor.clear.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
//                bottomButton.setTitle("无门槛", for: .normal)
            }
        }
    }
    
    var bottomButtonClick:((_:String,_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "saveMoneyCoupon")
        bgImageView.isUserInteractionEnabled = true
        contentView.addSubview(bgImageView)
        
        titleLabel = HWLabel.createLabel(title: "", color: "#FFE9B7", font: 9*widthScale)
        bgImageView.addSubview(titleLabel)
        
        priceLabel = HWLabel.createBoldLabel(title: "￥50", color: "#FFE9B7", font: 13*widthScale)
        bgImageView.addSubview(priceLabel)
        
        bottomButton = UIButton(type: .custom)
        bottomButton.titleLabel?.font = .boldSystemFont(ofSize: 11*widthScale)
        bottomButton.setTitle("无门槛", for: .normal)
        bottomButton.layer.cornerRadius = 11*widthScale
        bottomButton.layer.masksToBounds = true
        bottomButton.addTarget(self, action: #selector(bottomButtonClick(_:)), for: .touchUpInside)
        bgImageView.addSubview(bottomButton)
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(7*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(0)
            make.height.equalTo(25*widthScale)
        }
        
        bottomButton.snp.makeConstraints { make in
            make.bottom.equalTo(-5*widthScale)
            make.centerX.equalToSuperview()
            make.height.equalTo(22*widthScale)
            make.left.right.equalToSuperview()
        }
    }
    
}

extension HWSaveMoneyCouponCollentionCell  {
    @objc func bottomButtonClick(_:UIButton) -> Void {
        if model?.is_buy == "1" {
            
            if indexPath.row == 0 {
                if model?.zhou_data.coupon[indexPath.row].is_lq == "1" {
                    
                }else {
                    bottomButtonClick?(model?.zhou_data.coupon[indexPath.row].id ?? "","")
                }
                
            }else{
                if model?.zhou_data.coupon[indexPath.row].is_lq == "1" {
                    
                }else {
                    bottomButtonClick?(model?.zhou_data.coupon[indexPath.row].id ?? "",model?.yue_data.coupon[indexPath.row].day ?? "")
                }
                
            }
        } else if model?.is_buy == "2" {
            if indexPath.row == 0 {
                if model?.yue_data.coupon[indexPath.row].is_lq == "1" {
                    
                }else {
                    bottomButtonClick?(model?.yue_data.coupon[indexPath.row].id ?? "","")
                }
                
            }else{
                if model?.yue_data.coupon[indexPath.row].is_lq == "1" {
                    
                }else {
                    bottomButtonClick?(model?.yue_data.coupon[indexPath.row].id ?? "",model?.yue_data.coupon[indexPath.row].day ?? "")
                }
                
            }
        }else {
            
        }
    }
}
